package cm.iccsoft.android.telecomcost.entities;

import java.util.ArrayList;

import android.util.Log;

public final class Zone extends Entity {
	public Zone() {
		_type = PROP_TYPE;
		_countries = new ArrayList<Country>();
	}

	/**
	 * @param name
	 */
	public Zone(String name) {
		super();
		this.name = name;
		_type = PROP_TYPE;
		_countries = new ArrayList<Country>();
	}

	private ArrayList<Country> _countries;

	@Override
	public String getType() {
		// TODO Auto-generated method stub
		return PROP_TYPE;
	}

	@Override
	public void setType(String _type) {
	}	

	public void setCountries(ArrayList<Country> value) {
		this._countries = value;
	}

	public ArrayList<Country> getCountries() {
		return _countries;
	}

	public static final String PROP_TYPE = "zone";

	public boolean contains(Country _country) {
		int index = 0;

		while (index < _countries.size()) {
			Country country = _countries.get(index);
			if (country.getId() == _country.getId()) {
				return true;
			}
			
			index++;
		}

		return false;
	}

	public boolean remove(Country _country) {
		Log.i("Zone",
				"removing country " + _country.getName() + " from zone "
						+ getName());
		ArrayList<Country> newCountries = new ArrayList<Country>();
		
		int index = 0;

		while (index < _countries.size()) {
			Country country = _countries.get(index);
			if (country.getId() != _country.getId()) {
				newCountries.add(country);
			}
			
			index++;
		}

		_countries = newCountries;
		
		return !contains(_country);
	}
	
	public boolean add(Country _country)
	{
		if(remove(_country))
		{
			_countries.add(_country);
		}		
		
		return contains(_country);
	}
	
	public void clean()
	{
		ArrayList<Country> zoneCountries = new ArrayList<Country>();
		
		int index = 0;
		while(index < getCountries().size())
		{
			Country _country = getCountries().get(index);
			if(_country.getName() != null)
			{
				zoneCountries.add(_country);
			}
		}
		
		setCountries(zoneCountries);
	}
}
